home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / filelist.arc / FILELIST.C < prev    next >
C/C++ Source or Header  |  1991-04-17  |  12KB  |  449 lines

  1. /* Filelist.c -- Public Domain -- By Erik Vanriper -- SoWhutWare 1991
  2. **
  3. ** Why?  Because I had nothing better to do.
  4. **
  5. ** I probably could have done this 10 other ways, but I figured this works,
  6. ** so why break it.  The only thing left to add is the wildcard expansion
  7. ** on files.  Maybe tomorrow.....
  8. **
  9. ** I used Turbo C 2.01 on this, hack it to your hearts desire.
  10. **
  11. ** If you use a function, please give credit, I know I did... Thanx Bob Stout!
  12. **
  13. ** gotoxy(over, down);  (Just a reminder for myself...)
  14. **
  15. ** If the code format looks wierd, I used a tab stop of 3, and QEdit.
  16. **
  17. ** I also used TheDraw 4.01 to create the initial screen, FILELIST.ANS, and
  18. ** saved it under C, and Normal, everything else is default, filename is
  19. ** FILELIST.H.
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <alloc.h>
  26. #include <dir.h>
  27. #include <sys\types.h>
  28. #include <sys\stat.h>
  29. #include <time.h>
  30. #include <process.h>
  31. #include <graphics.h>
  32. #include "filelist.h"
  33.  
  34. #define NUL '\0'  /* This is part of Bob Stouts "commafmt()" code */
  35.  
  36. struct xx
  37. {
  38.    char FilePath[80];   /* 80 chars for the path and filename, */
  39.    char AreaDesc[200];  /* 200 chars for the description.      */
  40. };
  41.  
  42. struct xx *path[80];    /* Max of 80 areas to malloc() 22,400 bytes. */
  43. struct stat info;
  44. struct tm *tmfile;
  45. struct ffblk fileinfo;
  46.  
  47. char drive[MAXDRIVE];
  48. char dir[MAXDIR];
  49. char filename[MAXFILE];
  50. char ext[MAXEXT];
  51. char filepath[MAXPATH];
  52.  
  53. int tempcount = 0;
  54. int start = 0;
  55. int numfiles[80];
  56. long numbytes, areabytes;
  57.  
  58. char descrip[200], keeppath[200];
  59. char *whitespace = " \t\n\r";
  60. char input[80] = "FILELIST.CFG";  /* Default config file */
  61.  
  62. FILE *outfile;
  63.  
  64. main(int argc, char *argv[])
  65. {
  66.    FILE *fp;
  67.    char tempbuff[100];
  68.    int t;
  69.    numbytes = areabytes = 0;
  70.  
  71.    window(1,1,80,25);
  72.  
  73.    for(t=0; t < 80; t++)
  74.    {
  75.       if((path[t] = malloc(sizeof(struct xx))) == NULL)
  76.       {
  77.          puts("not enough memory!  aborting...");
  78.          exit(255);
  79.       }
  80.    }
  81.  
  82.    puttext(1,1,80,25,IMAGEDATA);  /* Throw up our screen */
  83.    textbackground(BLUE);
  84.    textcolor(LIGHTCYAN);
  85.  
  86.    if(argc > 1)                 /* assume that argv[1] is a valid config file */
  87.    {
  88.       strcpy(input, argv[1]);
  89.       gotoxy(14,23);
  90.       textcolor(YELLOW);
  91.       cprintf("Using configuration file %s",input);
  92.       textcolor(LIGHTCYAN);
  93.    }
  94.  
  95.    parsectl();                  /* parse the control file */
  96.  
  97.    /* Normalize the first two paths for reading and writing */
  98.    fnsplit(path[0]->FilePath, drive, dir, filename, ext);
  99.    fnmerge(path[0]->FilePath, drive, dir, filename, ext);
  100.    fnsplit(path[1]->FilePath, drive, dir, filename, ext);
  101.    fnmerge(path[1]->FilePath, drive, dir, filename, ext);
  102.  
  103.    if((outfile = fopen(path[0]->FilePath, "wt")) == NULL) /* open output file */
  104.    {
  105.       gotoxy(14,22);
  106.       cprintf("Error opening %s                                    ",path[0]->FilePath);
  107.       exit(255);
  108.    }
  109.  
  110.    if((fp = fopen(path[1]->FilePath, "rt")) == NULL) /* open header file */
  111.    {
  112.       gotoxy(14,22);
  113.       cprintf("Error opening %s                                    ",path[1]->FilePath);
  114.       exit(255);
  115.    }
  116.  
  117.    while(fgets(tempbuff,100,fp) != NULL)  /* write header file line by line */
  118.        fputs(tempbuff,outfile);
  119.  
  120.    fclose(fp);            /* Close header file */
  121.    listfiles();           /* process each FILES.BBS */
  122.  
  123.    for(t=0;t<80;t++)  free(path[t]);   /* free the memory */
  124.  
  125.    fclose(outfile);                    /* close the output file */
  126.    gotoxy(1,24);                       /* put cursor at bottom of screen */
  127.    return(1);                          /* all done */
  128. }
  129.  
  130. /*
  131. **  commafmt()
  132. **
  133. **  Public domain by Bob Stout
  134. **
  135. **  Notes:  1. Use static buffer to eliminate error checks on buffer overflow
  136. **             and reduce code size.
  137. **          2. By making the numeric argument a long and prototyping it before
  138. **             use, passed numeric arguments will be implicitly cast to longs
  139. **             thereby avoiding int overflow.
  140. **          3. Use the thousands grouping and thousands separator from the
  141. **             ANSI locale to make this more robust.
  142. */
  143.  
  144.  
  145.  
  146. /* Buffer for formatted string  */
  147. /* Size of buffer               */
  148. /* Number to convert            */
  149.  
  150. commafmt(char *buf, int bufsize, long N)
  151. /*size_t commafmt(char *buf, int bufsize, long N)*/
  152. {
  153.    int len = 1, posn = 1, sign = 1;
  154.    char *ptr = buf + bufsize - 1;
  155.  
  156.    if (2 > bufsize)
  157.    {
  158. ABORT:          *buf = NUL;
  159.                 return 0;
  160.    }
  161.  
  162.    *ptr-- = NUL;
  163.    --bufsize;
  164.    if (0L > N)
  165.    {
  166.       sign = -1;
  167.       N = -N;
  168.    }
  169.  
  170.    for ( ; len <= bufsize; ++len, ++posn)
  171.    {
  172.       *ptr-- = (char)((N % 10L) + '0');
  173.       if (0L == (N /= 10L))
  174.       break;
  175.  
  176.       if (0 == (posn % 3))
  177.       {
  178.          *ptr-- = ',';
  179.          ++len;
  180.       }
  181.  
  182.       if (len >= bufsize)  goto ABORT;
  183.    }
  184.  
  185.    if (0 > sign)
  186.    {
  187.       if (0 == bufsize)    goto ABORT;
  188.       *ptr-- = '-';
  189.    }
  190.  
  191.    strcpy(buf, ++ptr);
  192.    /*return (size_t)len;*/
  193. }
  194.  
  195. int listfiles()
  196. {
  197.    FILE *fp;
  198.    static int i, j, count, test = 0, test2 = 0;
  199.    char desc[200];
  200.    char buf[20];
  201.  
  202.    gotoxy(27,5);
  203.    cprintf("%d",(start - 2));
  204.    gotoxy(3,8);
  205.    textcolor(LIGHTGREEN);
  206.    cprintf("Writing Output to     :");
  207.    textcolor(LIGHTCYAN);
  208.    gotoxy(27,8);
  209.    cprintf("%s",path[0]->FilePath);
  210.    for(i=2;i<start;i++)
  211.    {
  212.       test = strlen(path[i]->FilePath);
  213.       gotoxy(27,4);
  214.       cprintf("%d %s",(i-1),path[i]->FilePath);
  215.       strcpy(keeppath,path[i]->FilePath);
  216.       strcat(path[i]->FilePath,"files.bbs");
  217.  
  218.       if((fp = fopen(path[i]->FilePath,"rt")) == NULL)
  219.       {
  220.          gotoxy(14,22);
  221.          cprintf("Error: Can't find file %s                             ",path[i]->FilePath);
  222.          continue;
  223.       }
  224.  
  225.       fprintf(outfile,"\n\n-=- Area: %s\n",path[i]->AreaDesc);
  226.       test2 = strlen(path[i]->AreaDesc);
  227.  
  228.       gotoxy(3,10);
  229.       textcolor(LIGHTRED);
  230.       cprintf("%s",path[i]->AreaDesc);
  231.       textcolor(LIGHTCYAN);
  232.       gotoxy(1,1);
  233.  
  234.       count = areabytes = 0;
  235.  
  236.       while(fgets(desc,200,fp) != NULL)
  237.          if(processline(desc)) count++;
  238.  
  239.       numfiles[i] = count;
  240.       tempcount += count;
  241.  
  242.       if(i < 10)
  243.       {
  244.          gotoxy((test + 29), 4);
  245.          for(j = 0; j < (test + 2); j++)
  246.             cprintf("\b \b");
  247.       }
  248.       else
  249.       {
  250.          gotoxy((test + 30), 4);
  251.          for(j = 0; j < (test + 3); j++)
  252.             cprintf("\b \b");
  253.       }
  254.  
  255.       gotoxy((test2 + 3), 10);
  256.       for(j=0;j<test2; j++)  cprintf("\b \b");
  257.  
  258.       if(count)
  259.       {
  260.          commafmt(buf,20,areabytes);
  261.          fprintf(outfile,"\n=-= Area Stats: %s Bytes in %d Files",buf,count);
  262.          fprintf(outfile,"\n************************************************************************");
  263.          gotoxy(27,7);
  264.          textcolor(WHITE);
  265.          cprintf("%d",tempcount);
  266.          commafmt(buf,20,numbytes);
  267.          gotoxy(27,6);
  268.          textcolor(LIGHTMAGENTA);
  269.          cprintf("%s",buf);
  270.          textcolor(LIGHTCYAN);
  271.       }
  272.       else
  273.       {
  274.          fprintf(outfile,"\n=-= Area Stats: NO FILES");
  275.          fprintf(outfile,"\n************************************************************************");
  276.       }
  277.  
  278.       fclose(fp);
  279.    }
  280.  
  281.    count = 0;
  282.    
  283.    gotoxy(14,22);
  284.    cprintf("Computing Statistics...                        ");
  285.  
  286.    fprintf(outfile,"\n=-=-=-=-=-=-=\nFinal Report:\n=-=-=-=-=-=-=\n");
  287.  
  288.    for(i=2;i<start;i++)
  289.    {
  290.       fprintf(outfile,"       %3d Files in%s",numfiles[i],path[i]->AreaDesc);
  291.       count += numfiles[i];
  292.    }
  293.  
  294.    commafmt(buf,20,numbytes);
  295.  
  296.    gotoxy(27,7);
  297.    textcolor(WHITE);
  298.    cprintf("%d", count);
  299.    gotoxy(27,6);
  300.    textcolor(LIGHTMAGENTA);
  301.    cprintf("%s",buf);
  302.    textcolor(LIGHTCYAN);
  303.  
  304.    fprintf(outfile,"      ----\n      %4d Total Files in %s bytes",count, buf);
  305.    fprintf(outfile,"\n* Produced automatically by FILELIST  (1991)  Erik Vanriper.  (1:260/230)\n");
  306.    gotoxy(27,4);
  307.    cprintf("Done!");
  308.    gotoxy(14,22);
  309.    cprintf("Thank you for using FILELIST.  Pass it on to your friends!     ");
  310.    return(1);
  311. }
  312.  
  313. void killspace(char buffer[200])
  314. {
  315.    int test;
  316.  
  317.    test = strspn(buffer,whitespace);
  318.    strcpy(descrip,(buffer + test));
  319. }
  320.  
  321. int processline(char desc[200])
  322. {
  323.    char filename[13], *test, check[80];
  324.    int result, c, i, d;
  325.  
  326.    if((desc[0] < '\x30') || (desc[0] > '\x7A') || ((desc[0] > '\x3A') && (desc[0] < '\x41')) || ((desc[0] > '\x5A') && (desc[0] < '\x61')))
  327.    {
  328.       fputs(desc,outfile);
  329.       return(0);
  330.    }
  331.    test = strstr(desc, "\x20");
  332.    strcpy(descrip,test);
  333.    result = strlen(descrip);
  334.    i = strlen(desc);
  335.    strncpy(filename,desc,(i - result));
  336.    filename[(i-result)] = '\0';
  337.  
  338.    strcpy(check,keeppath);
  339.    strcat(check,"\\");
  340.    strcat(check,filename);
  341.  
  342.    killspace(descrip);
  343.  
  344.    if(stat(check, &info) !=0)   return(0);
  345.  
  346.    numbytes += info.st_size;
  347.    areabytes += info.st_size;
  348.  
  349.    result = strlen(filename);
  350.  
  351.    fprintf(outfile,"%s",filename);
  352.  
  353.    for(c=0;c<(13-result);c++) fprintf(outfile," ");
  354.  
  355.    fprintf(outfile,"%ld",info.st_size);
  356.  
  357.    tmfile = localtime(&info.st_atime);
  358.  
  359.         if(info.st_size <= 999)  fprintf(outfile,"     %.2d-%.2d-%d  ",(tmfile->tm_mon +1),tmfile->tm_mday,tmfile->tm_year);
  360.    else if(info.st_size <= 9999)  fprintf(outfile,"    %.2d-%.2d-%d  ",(tmfile->tm_mon +1),tmfile->tm_mday,tmfile->tm_year);
  361.    else if(info.st_size <= 99999)  fprintf(outfile,"   %.2d-%.2d-%d  ",(tmfile->tm_mon +1),tmfile->tm_mday,tmfile->tm_year);
  362.    else fprintf(outfile,"  %.2d-%.2d-%d  ",(tmfile->tm_mon +1),tmfile->tm_mday,tmfile->tm_year);
  363.  
  364.    writedescrip();
  365.  
  366.    return(1);
  367. }
  368.  
  369. writedescrip()
  370. {
  371.    char *token;
  372.    int test, x = 0;
  373.  
  374.    test = strlen(descrip);
  375.    if(test <= 47)
  376.    {
  377.       fprintf(outfile,"%s",descrip);
  378.       return(1);
  379.    }
  380.    else
  381.    {
  382.       token = strtok(descrip,whitespace);
  383.  
  384.       while(token != NULL)
  385.       {
  386.          x = x + strlen(token);
  387.  
  388.          if(x <= 47)
  389.          {
  390.             fprintf(outfile,"%s ",token);
  391.             x = x + 1;
  392.          }
  393.  
  394.          else
  395.          {
  396.             x = 1;
  397.             fprintf(outfile,"\n                               %s ",token);
  398.          }
  399.  
  400.          token = strtok(NULL,whitespace);
  401.       }
  402.    }
  403.    fprintf(outfile,"\n");
  404.    return(1);
  405. }
  406.  
  407. int parsectl()
  408. {
  409.    FILE *fp;
  410.    int result, c, i, d;
  411.    char wholeline[200], *test;
  412.  
  413.    if ((fp = fopen(input,"rt")) == NULL)
  414.    {
  415.       gotoxy(14,22);
  416.       cprintf("Can't find file %s                                   ",input);
  417.       exit(255);
  418.    }
  419.  
  420.    gotoxy(14,22);
  421.    cprintf("Parsing configuration file...");
  422.  
  423.    c = 0;
  424.    while(fgets(wholeline,200,fp) != NULL)
  425.    {
  426.       test = strstr(wholeline, "\x20");
  427.       
  428.       if(test == NULL)
  429.       {
  430.          strcpy(path[c]->FilePath,wholeline);
  431.          d = strlen(path[c]->FilePath);
  432.          path[c]->FilePath[d] = '\0';
  433.       }
  434.       else
  435.       {
  436.          strcpy(path[c]->AreaDesc,test);
  437.          result = strlen(wholeline);
  438.          i = strlen(path[c]->AreaDesc);
  439.          strncpy(path[c]->FilePath,wholeline,(result - i));
  440.          path[c]->FilePath[(result - i)] = '\0';
  441.       }
  442.       c++; start++;
  443.    }
  444.    fclose(fp);
  445.    gotoxy(14,22);
  446.    cprintf("Working...                    ");
  447.    return(1);
  448. }
  449.